package com.eboss.sbs.activity;

import android.os.Bundle;
import android.view.View;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.security.Base64;
import com.mytools.tool.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

public class PasswordModifyActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_password_modify);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "修改密码");
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.btn_submit).setOnClickListener(this);
		
	}
	
	private void doSubmit(){
		String password_old = getTvText(R.id.tv_password_old);
		String password_new = getTvText(R.id.tv_password_new);
		String password_confirm = getTvText(R.id.tv_password_confirm);
		if(StringUtil.isNull(password_old)){
			toast("当前密码不能为空!");
			return;
		}
		if(StringUtil.isNull(password_new)){
			toast("当前密码不能为空!");
			return;
		}
		if(StringUtil.isNull(password_confirm)){
			toast("当前密码不能为空!");
			return;
		}
		if(!password_new.equals(password_confirm)){
			toast("两次密码输入不一致！");
			return;
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("oldPassword", Base64.getBase64(password_old));
		params.put("newPassword", Base64.getBase64(password_new));
		API.http_post(this, API.MODIFY_PASSWORD, params, new HttpCallback<Object>(this) {
			@Override
			public void onSuccess(int statusCode, Object data) {
				mApp.shareUtil.removeSharePara(LoginActivity.LOGIN_PASSWORD);
				toast("密码修改成功！");
				finish();
			}
			@Override
			public void onFailure(int statusCode, String message) {
				super.onFailure(statusCode, message);
			}
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<Object>() {
				};
			}
		});
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.btn_submit:
				doSubmit();
				break;
			default: break;
		}
		super.onClick(v);
	}
	
}
